<?php
class Close extends Controller {
	function Close()  {
		parent::Controller();
		$this->load->helper("url");
		$this->data["base"] = $this->config->item('base_url');
		$this->load->model('Ticketmodel','',TRUE);
		$this->load->model('Translog');
		$this->load->database();	
                session_start();
	}
	var $limit =3;
	function index() {
		if(((isset($_SESSION['sys_admin'])) and isset($_SESSION['name']))||((isset($_SESSION['admin']))and isset($_SESSION['name']))||((isset($_SESSION['intern_audit']))and isset($_SESSION['name']))||((isset($_SESSION['second_consult']))and isset($_SESSION['name']))||((isset($_SESSION['dept_pic']))and isset($_SESSION['name']))) {
			$this->get_last_ten_berita();
		} else {
			redirect('main');
		}
	}
	
	/* CLOSE TICKET
	--------------------------------------------------------------------------------*/
	
	function get_last_ten_berita($offset = 0) {
		$this->data['title'] ='Close Ticket';
		//offset
		$uri_segment = 3;
		$offset = $this->uri->segment($uri_segment);
		$this->data['ticket'] =$this->Ticketmodel->close($offset);
		// load data table berita_utama
		$tickets = $this->Ticketmodel->close($offset)->result();
		$num_rows = $this->Ticketmodel->count_all_num_rows();
		
		if($num_rows > 0) { // jika query menghasilkan data
			// membuat pagination
			$config['base_url'] = site_url('createticket/mngticket/get_last_ten_berita');
			$config['total_rows'] = $num_rows;
			$config['per_page'] = $this->limit;
			$config['uri_segment'] = $uri_segment;
			$this->pagination->initialize($config);
			$data['pagination'] = $this->pagination->create_links();
			
			//set template table untuk efek selang-sling tiap baris
			$tmpl = array('table_open' =>'<table border="0" cellpadding="0" cellspacing="0" width="880">','row_alt_start' =>'<tr class="tb-jd">','row_alt_end' =>'</tr>');
			$this->table->set_template($tmpl);
			
			//set heading untul tabel
			$this->table->set_empty("&nbsp;");
			$this->table->set_heading('No','Ticket ID','Name','Open  Datetime','Target Date','Close Date','Status','Severity Level','Product','Actions');
			
			//Penomeran baris data
			$this->data['i'] = 0 + $offset;
			$i = 0 + $offset;
			foreach($tickets as $ticket) {
				
				
				//penyusunan data baris perbaris
				
				$this->table->add_row(++$i,$ticket->ticket_id,$ticket->name,$ticket->open_datetime,$ticket->target_date,$ticket->close_date,$ticket->status,$ticket->severity_level,$ticket->product,
				anchor('createticket/close/req_close_ticket/'.$ticket->ticket_id,'&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp &nbsp; &nbsp; &nbsp; ',array('class'=>'close','onclick'=>"return confirm('Anda yakin akan mengclose ticket ini?')")));
				
			}
			$this->data['table'] = $this->table->generate();
		}
		else {
			$this->data['message'] = 'tidak di temukan satupun data ticket utama';
		}
		$this->data['link'] = array('link_add'=>anchor('createticket/ticket/','Create Ticket',array('class' =>'add')));
		
		//load default view
		$this->load->vars($this->data);
		$this->load->view('layout/head');
		$this->load->view('layout/body');
		$this->load->view('layout/slice-atas');
		$this->load->view('ticket/req_close_ticket');
		$this->load->view('layout/slice_bawah');	
	}
	/* END CLOSE
	---------------------------------------------------------------------------------*/
	function close_ticket($id_update ) {
		$show =$this->data['update'] = $this->Ticketmodel->update_manage($id_update)->row();
		$c =date('Y-m-d H:i:s');
		$d ="Close";
		$a = $id_update;
		$b = array('close_date' =>date('Y-m-d H:i:s',strtotime($c)),
					'status' =>$d);
		
		$this->Ticketmodel->close_ticket($a,$b);
		
		$this->data['message'] = 'berhasil close';
		
		//load default view
		redirect('createticket/close');
		
	}
	function req_close_ticket($id_update ) {
		$show =$this->data['update'] = $this->Ticketmodel->update_manage($id_update)->row();
		$d ="Request to Close";
		$a = $id_update;
		$b = array('status' =>$d);
		
		$this->Ticketmodel->close_ticket($a,$b);
		
		$this->data['message'] = 'berhasil close';
		
		//load default view
		redirect('createticket/close');
		
	}
}
?>